package org.example.lys.date;

import lombok.extern.slf4j.Slf4j;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;

/**
 * @author liyishan
 * @date 2023/5/26 13:08
 * @apiNote
 */
@Slf4j
public class DateUtils {
    public static final String yyyy = "yyyy";
    public static final String MM = "MM";
    public static final String dd = "dd";
    public static final String yyyyMMdd = "yyyyMMdd";
    public static final String MMDDHHMMSS = "MMddHHmmss";
    public static final String yyyy_MM_ddHHmmss = "yyyy-MM-dd HH:mm:ss";
    public static final String yyyy_MM_ddHHmm = "yyyy-MM-dd HH:mm";
    public static final String yyyy_MM_dd = "yyyy-MM-dd";
    public static final String yyyyMMdd2 = "yyyy 年 MM 月 dd 日";
    public static final String yyyyMMdd3 = "yyyy年MM月dd日";
    public static final String yyyy_MM = "yyyy-MM";

    public static final String ZERO = " 00:00:00";
    public static final String DAY_END = " 23:59:59";

    public DateUtils() {
    }

    //某个时间的前day天
    public static Date getDateBefore(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(5, now.get(5) - day);
        return now.getTime();
    }

    //某个时间的前hour个小时
    public static Date getDateBeforeHour(Date d, int hour) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(d);
        calendar.set(11, calendar.get(11) - hour);
        new SimpleDateFormat(yyyy_MM_ddHHmmss);
        return calendar.getTime();
    }

    //某个时间的后x分钟
    public static Date addDateMinut(Date date, int x) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(12, x);
        date = cal.getTime();
        cal = null;
        return date;
    }

    //某个时间的后几天
    public static Date getDateAfter(Date d, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(d);
        now.set(5, now.get(5) + day);
        return now.getTime();
    }

    public static String getDateFormatter() {
        Date date = new Date();
        SimpleDateFormat df = new SimpleDateFormat(yyyy_MM_ddHHmmss);
        return df.format(date);
    }


    public static String format(String pattern) {
        return new SimpleDateFormat(pattern).format(new Date());
    }

    // 把date 转换成指定格式的string
    public static String formatDate(Date date, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.format(date);
    }

    // 把字符串按照指定格式转换成date
    public static Date parse(String dateStr, String format) {
        SimpleDateFormat df = new SimpleDateFormat(format);
        Date date = null;
        try {
            date = df.parse(dateStr);
        } catch (ParseException var5) {
            var5.printStackTrace();
        }
        return date;
    }


    public static long getDateDifference(Date startTime, Date endTime, int type) {
        long nd = 1000 * 24 *60* 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        long ns = 1000L;
        long diff = endTime.getTime() - startTime.getTime();
        long day = diff / nd;
        long hour = diff% nd / nh +24 *(diff / nd);
        long min = diff / nm;
        long sec = diff % nd % nh % nm / ns;
        long result = 0L;
        switch (type) {
            case 1:
                result = day;
                break;
            case 2:
                result = hour;
                break;
            case 3:
                result = min;
                break;
            case 4:
                result = sec;
        }

        return result;
    }


    public static int daysBetween(String maxTime, String minTime) {
        Date max = parse(maxTime, yyyy_MM_ddHHmm);
        Date min = parse(minTime, yyyy_MM_ddHHmm);
        return daysBetween(max, min);
    }

    public static int daysBetween(Date maxTime, Date minTime) {
        long max = maxTime.getTime();
        long min = minTime.getTime();
        Long day = Long.valueOf((max - min) / 86400000L);
        return day.intValue();
    }

    public static void main(String[] args) {
        Date start = new Date();
        Date end = getDateAfter(new Date(),1);
        int date = daysBetween(start,end);
        LocalDateTime now1 = LocalDateTime.now();
        LocalDateTime minus = now1.minus(1, ChronoUnit.MONTHS);

        System.out.println(minus);
    }
}
